package com.ruoyi.flow.common.config;

import com.ruoyi.flow.common.utils.PropertiesUtils;

/**
 * @Author: wtian
 * @Description:
 * @Date: Created in 10:41 2018/8/3
 * @Version: 1.0
 */
public class Global {
    static {
        System.out.println("=====================");
        System.out.println(Global.class.getResource("/").getPath() + "/workflow.properties");
        System.out.println("=====================");
    }
    private static final PropertiesUtils pu = PropertiesUtils.load(Global.class.getResource("/").getPath() + "/workflow.properties");

    public static String getConfig(String key) {
        return getString(key, null);
    }

    /**
     * 获取字符串值
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getString(String key, String defaultValue) {
        return pu.getString(key, defaultValue);
    }

    /**
     * 获取int数值
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static int getInt(String key, int defaultValue) {
        return pu.getInt(key, defaultValue);
    }

    /**
     * 获取long数值
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static long getLong(String key, long defaultValue) {
        return pu.getLong(key, defaultValue);
    }

    /**
     * 获取boolean类型值
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static boolean getBoolean(String key, boolean defaultValue) {
        return pu.getBoolean(key, defaultValue);
    }

    /**
     * Get double type value.
     *
     * @param key
     * @param defaultValue
     * @return
     */

    public static double getDouble(String key, double defaultValue) {
        return pu.getDouble(key, defaultValue);
    }

    /**
     * 获取对象
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static <T> T getObject(String key, Class<T> defaultValue) {
        return pu.getObject(key, defaultValue);
    }

    //***********************以下为了适配Enviroment***********************************//
    public static String getProperty(String key){
        return getString(key,null);
    }

    public static <T> T getProperty(String key, Class<T> tClass) {
        return (T) pu.getObject(key, null);
    }

    public static <T> T getProperty(String key, Class<T> tClass, T defaultValue) {
        T t = (T)pu.getProperties().get(key);
        if (t == null) {
            t = defaultValue;
        }
        return t;
    }
}
